import 'package:enjoy_plus/utils/http.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';

class NoticeDetail extends StatefulWidget {
  const NoticeDetail({super.key});

  @override
  State<NoticeDetail> createState() => _NoticeDetailState();
}

class _NoticeDetailState extends State<NoticeDetail> {
  Map notifyDetail = {};

  // 生命周期中 解析参数

  @override
  // initState 无法解析路由参数，会报错
  void didChangeDependencies() {
    // 调用父类的同名方法，保证原本的逻辑可以执行
    super.didChangeDependencies();
    // 如果不是跳转而来，那么 params应该是没有值的
    final params = ModalRoute.of(context)!.settings.arguments;
    if (params != null) {
      final id = (params as Map)['id'];
      // print(id);
      getContent(id);
    }
    // print(params['id']);
  }

  getContent(String id) async {
    final res = await http.get('/announcement/$id');
    print(res['data']);
    setState(() {
      notifyDetail = res['data'];
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('公告详情'),
        ),
        body: ListView(children: [
          Container(
              padding: const EdgeInsets.all(10),
              child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    // 标题
                    Text(notifyDetail['title'] ?? '',
                        style: const TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                        )),
                    const SizedBox(height: 10),
                    Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(notifyDetail['creatorName'] ?? '',
                              style: const TextStyle(color: Colors.grey)),
                          Text(notifyDetail['createdAt'] ?? '',
                              style: const TextStyle(color: Colors.grey))
                        ]),
                    const SizedBox(height: 10),
                    // 内容
                    // 通过第三方库 flutter_html 进行解析
                    Html(
                      data: notifyDetail['content'] ?? '',
                    )
                  ]))
        ]));
  }
}
